using System.Linq;
using Shop.Backend.Api.Entity;
using Shop.Backend.Api.Params;
using Shop.Backend.Api.Repository;
using Shop.Backend.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace Shop.Backend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    //这里的控制器是 GoodsBrands 品牌
    public class GoodsBrandsController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<Users> _usersRepository;
        private IRepository<Goods> _goodsRepository;
        private IRepository<Shops> _shopsRepository;
        private IRepository<GoodsBrands> _goodsBrandsRepository;

        private TokenParameter _tokenParameter;


        public GoodsBrandsController(IConfiguration configuration
        , IRepository<Users> usersRepository
        , IRepository<Goods> goodsRepository
        , IRepository<Shops> shopsRepository
        , IRepository<GoodsBrands> goodsBrandsRepository)
        {
            _configuration = configuration;
            _usersRepository = usersRepository;
            _goodsRepository = goodsRepository;
            _shopsRepository = shopsRepository;
            _goodsBrandsRepository = goodsBrandsRepository;
            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }


        [HttpGet]
        public dynamic Get([FromQuery] QueryWithPager query)
        {
            // get请求默认从url中获取参数，如果需要使用实体接收参数，需要FromQuery特性
            var pageIndex = query.Pager.PageIndex;
            var pageSize = query.Pager.PageSize;

            var keyword = string.IsNullOrEmpty(query.Keyword) ? "" : query.Keyword.Trim();

            var brands = _goodsBrandsRepository.Table.Where(x => x.IsDeleted == false); ;

            // 如果keyword不为空，则再去查询用户名
            if (!string.IsNullOrEmpty(keyword))
            {
                brands = brands.Where(x => x.Id.ToString() == keyword
              || x.BrandsName.Contains(keyword)
              || x.Remarks.Contains(keyword)); //未完善的模糊查询
            }
            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = new
                {
                    Data = brands,
                    Pager = new
                    {
                        pageIndex,
                        pageSize,
                        rowsTotal = brands.Count()
                    }
                },
                Msg = "获取品牌列表成功^_^"
            });
        }

        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var brand = _goodsBrandsRepository.GetById(id);
            return new
            {
                Code = 1000,
                Data = brand,
                Msg = "获取指定品牌成功^_^"
            };
        }

        [HttpPost]
        public dynamic Post(CreateBrand createBrand)
        {
            var brandsName = createBrand.BrandsName.Trim();
            var brandDesc = createBrand.BrandDesc.Trim();
            var brandLogo = createBrand.BrandLogo.Trim();


            var isBrand = _goodsBrandsRepository.Table.Where(x => x.BrandsName == createBrand.BrandsName);
            if (isBrand.Count() > 0)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "该品牌已存在^_^"
                };
            }

            if (string.IsNullOrEmpty(brandsName))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "品牌名不能为空"
                };
            }
            if (string.IsNullOrEmpty(brandDesc))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "描述不能为空"
                };
            }
            if (string.IsNullOrEmpty(brandLogo))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "Logo不能为空"
                };
            }

            var goodsBrands = new GoodsBrands
            {
                BrandsName = createBrand.BrandsName,
                BrandDesc = createBrand.BrandDesc,
                BrandLogo = createBrand.BrandLogo
            };

            _goodsBrandsRepository.Insert(goodsBrands);
            return new
            {
                Code = 1000,
                Data = goodsBrands,
                Msg = "添加品牌成功^_^"
            };
        }
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {

            var brand = _goodsBrandsRepository.GetById(id);

            if (brand == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "删除品牌不存在O.O"
                };

            }

            brand.IsActived = false;
            brand.IsDeleted = true;
            _goodsBrandsRepository.Update(brand);

            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除品牌成功^_^"

            };
        }
        //修改品牌状态的方法
        [HttpPut,Route("status")]
        public dynamic PutStatus(CreateBrand updateBrand)
        {
            var id = updateBrand.id;
            var user = _goodsBrandsRepository.GetById(id);
            
            
            if (user == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "要更新的品牌不存在，请确认后重试=_="
                };
            }

            user.IsActived = updateBrand.IsActived;
            _goodsBrandsRepository.Update(user);

            return new
            {
                Code = 1000,
                Data = user,
                Msg = "更新成功"
            };
        }

        //编辑指定品牌
        [HttpPut("{id}")]
        public dynamic Put(int id, CreateBrand updatebrand)//未完善
        {
            var brandsName = updatebrand.BrandsName.Trim();
            var goodsName = updatebrand.BrandDesc.Trim();

            //拿到传过来的图片路径
            var brandLogo = updatebrand.BrandLogo.Trim();

            //截取最后一个斜杠后面的字符串,取得相对路径
            string filename = System.IO.Path.GetFileName(brandLogo);
            

            var isBrand = _goodsBrandsRepository.Table.Where(x => x.BrandsName == updatebrand.BrandsName && x.Id != id);
            if (isBrand.Count() > 0)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "该品牌已存在^_^"
                };
            }
            if (string.IsNullOrEmpty(brandsName))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "品牌名不能为空"
                };
            }
           
            var brand = _goodsBrandsRepository.GetById(id);

            if (brand == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "品牌名不存在"
                };
            }

            brand.BrandsName = updatebrand.BrandsName;
            brand.BrandDesc = updatebrand.BrandDesc;
            brand.BrandLogo = filename;

            _goodsBrandsRepository.Update(brand);

            return new
            {
                Code = 1000,
                Data = brand,
                Msg = "编辑品牌成功^_^"
            };

        }

    }
}